﻿using System;
using System.IO;
using System.Reflection;

namespace Netfraction.Common.DataStorage
{
    public abstract class DataStore : IDataStore
    {
        private static string _applicationDataDir;

        private DataStoreType _dataStoreType;
        private string _dataStoreName;
        private DataSource _owner;
        private string _basePath;

        public string FullPath
        {
            get
            {
                if (string.IsNullOrEmpty(_dataStoreName))
                    return string.Empty;

                return Path.Combine(_basePath, _dataStoreName);
            }
        }

        public string BasePath
        {
            get { return _basePath; }
        }

        public DataStore(DataSource owner)
        {
            this._owner = owner;
            this._dataStoreName = string.Empty;

            if (string.IsNullOrEmpty(_applicationDataDir))
            {
                Assembly curAssembly = System.Reflection.Assembly.GetEntryAssembly();
                string curCompany = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), GetAssemblyCompany(curAssembly));
                _applicationDataDir = System.IO.Path.Combine(curCompany, GetProductName(curAssembly));
            }

            this._basePath = Path.Combine(_applicationDataDir, this.Owner.Name + "." + this.Owner.Guid.ToString() + Path.DirectorySeparatorChar +
                this.Owner.Version + Path.DirectorySeparatorChar);
        }

        // if the name is a path, make sure it's root is in the data path.
        protected bool CheckBasePath(string name)
        {
            if (!Path.GetFullPath(Path.Combine(this.BasePath, name)).StartsWith(this.BasePath))
                return false;
            else
                return true;
        }

        public DataSource Owner
        {
            get { return _owner; }
            set
            {
                if (string.IsNullOrEmpty(_owner.Version))
                    _owner.Version = "";

                _owner = value;
            }
        }

        public string DataStoreName
        {
            get { return _dataStoreName; }
            set { _dataStoreName = value; }
        }

        public DataStoreType DataStoreType
        {
            get { return _dataStoreType; }
            set { _dataStoreType = value; }
        }

        #region IDataStore Members

        bool IDataStore.Create(string name, bool overwrite)
        {
            return false;
        }

        bool IDataStore.Delete()
        {
            return false;
        }

        #endregion

        #region GetAppDir

        /// <summary>
        /// Gets the name of the product.
        /// </summary>
        /// <param name="assembly">The assembly.</param>
        /// <returns></returns>
        public static string GetProductName(Assembly assembly)
        {
            string productName = "";

            if (Attribute.IsDefined(assembly, typeof(AssemblyProductAttribute)))
            {
                AssemblyProductAttribute asProductName = (AssemblyProductAttribute)Attribute.GetCustomAttribute(assembly, typeof(AssemblyProductAttribute));
                productName = asProductName.Product;
            }

            return productName;
        }

        /// <summary>
        /// Gets the assembly company.
        /// </summary>
        /// <param name="assembly">The assembly.</param>
        /// <returns></returns>
        public static string GetAssemblyCompany(Assembly assembly)
        {
            string company = "";

            if (Attribute.IsDefined(assembly, typeof(AssemblyCompanyAttribute)))
            {
                AssemblyCompanyAttribute ascompany = (AssemblyCompanyAttribute)Attribute.GetCustomAttribute(assembly, typeof(AssemblyCompanyAttribute));
                company = ascompany.Company;
            }

            return company;
        }

        #endregion
    }
}
